//
//  DemoBase.m
//  MultiperThreadTest02
//
//  Created by FelixYin on 2020/4/4.
//  Copyright © 2020 FelixYin. All rights reserved.
//

#import "DemoBase.h"

@interface DemoBase ()

@property (nonatomic,assign) NSInteger num;
@property (nonatomic,assign) NSInteger money;

@end

@implementation DemoBase

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.money = 10000;
        self.num = 1000;
    }
    return self;
}

- (void) saveMoney{
    sleep(0.2);
    self.money+=50;
    NSLog(@"存了50，当前还剩 === %@",@(self.money));
}

- (void) drawMoney{
    sleep(0.2);
    self.money-=20;
    NSLog(@"取出20，当前还剩 === %@",@(self.money));
}


- (void) saveAndDrawMoney{
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        for (int i = 0; i < 20; i++) {
            [self saveMoney];
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 20; i++) {
            [self drawMoney];
        }
    });
}




// MARK:  卖票

- (void) saleTicket{
    sleep(0.2);
    self.num--;
    NSLog(@"还剩下 %@ 张票",@(self.num));
}

- (void) saleTickets{
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
         [self saleTicket];
        }
    });
    
    dispatch_async(queue, ^{
       for (int i = 0; i < 10; i++) {
         [self saleTicket];
       }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
          [self saleTicket];
        }
    });
}

@end
